package com.sander.guardmanagement.http;

import android.util.Log;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

/**
 * Created by sander on 2017/4/24.
 */

public class BaseRequestModel {


    public HashMap<String,String> init() {
        try {
            //"com.geocompass.model.STSTBPRPModel"
            HashMap<String, String> fieldHashMap = new HashMap<String, String>();
            Class cls = this.getClass();  //com.geocompass.model.STSTBPRPModel
            Field[] fieldlist = cls.getDeclaredFields();
            for (int i = 0; i < fieldlist.length; i++) {
                Field fld = fieldlist[i];
                Log.d("sander","type = " + fld.getName());
                Object o = getFieldValueByName(fld.getName());
                if (o != null){
                    fieldHashMap.put(fld.getName(), getFieldValueByName(fld.getName()));
                }
            }
            return fieldHashMap;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
    private String getFieldValueByName(String fieldName) {
        try {
            String firstLetter = fieldName.substring(0, 1).toUpperCase();
            String getter = "get" + firstLetter + fieldName.substring(1);
            Method method = this.getClass().getMethod(getter, new Class[] {});
            Object value = method.invoke(this, new Object[] {});
            return value.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    /**利用MD5进行加密
     * @param str  待加密的字符串
     * @return  加密后的字符串
     * @throws NoSuchAlgorithmException  没有这种产生消息摘要的算法
     * @throws UnsupportedEncodingException
     */
    public String EncoderByMd5(String str) {
        //确定计算方法
        try {
  MessageDigest md = MessageDigest.getInstance("MD5");
                         md.update(str.getBytes());
                         byte b[] = md.digest();

                         int i;

                         StringBuffer buf = new StringBuffer("");
                         for (int offset = 0; offset < b.length; offset++) {
                                 i = b[offset];
                                 if (i < 0)
                                         i += 256;
                                 if (i < 16)
                                         buf.append("0");
                                 buf.append(Integer.toHexString(i));
                             }
                         //32位加密
                         return buf.toString();
                         // 16位的加密
                         //return buf.toString().substring(8, 24);
                    } catch (NoSuchAlgorithmException e) {
                        e.printStackTrace();
            return null;
                    }

    }
    public Map<String,String> getValueOfClas()
    {
        Map<String,String> value_calss = init();
        Set<String> keyset  = value_calss.keySet();
        List<String> keylist = new ArrayList<>();
        for (String key : keyset){
            keylist.add(key);
        }

        Collections.sort(keylist);

        String map_string = "";

        for (int i = 0; i < keylist.size(); i ++){
            String key = keylist.get(i);
            String content = value_calss.get(key);
            map_string = map_string + key + "=" + content + "&";
        }
        map_string = map_string + "key=" + HttpApi.key_sh;
        Log.d("sander",map_string);
       String md5_string = this.EncoderByMd5(map_string);
        Log.d("sander","md5 = " + md5_string);
        value_calss.put("sign",md5_string.toUpperCase());

        return value_calss;

    }

    public String requestUrl(){
        return "";
    }
}
